let currentServCategId = 0;

/**
 * zmn渠道管理-服务分类
 * 文件命名格式：zmn.模块.表/功能.js
 * @author xiangjunming
 * @since 2019/08/22 18:06
 */
layui.define(['form', 'layedit', 'laydate','element'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var laydate = layui.laydate;
    var element = layui.element;

    form.render(null, "zmn-form");

    currentServCategId = json.currentServCategId;

    //监听Tab切换，以改变地址hash值
    element.on('tab(servCategId)', function(){
        currentServCategId = this.getAttribute('lay-id');
    });

    // 提交
    form.on('submit(zmn-form-submit)', function(obj){
        var data = obj.field;
        //获取checkbox[name='receivablesLimit']的值
        var arr = new Array();
        var index = 0;
        var serachStr = currentServCategId + "-";
        $("input:checkbox[name='categoryId']:checked").each(function(i){
            var currentValue = $(this).val();
            if (currentValue.indexOf(serachStr) == 0) {
                arr[index] = currentValue;
                index++;
            }
        });
        data.categoryId = arr.join(",");//将数组合并成字符串
        data.servCategId = currentServCategId;
        var url = "/channel/service/area/saveServiceCategory.action";
        $.ajax({
            async: false,
            type: "POST", url: url,
            dataType: "json", data: data,
            success:function(resp) {
                var message = resp.status == 200 ? "操作成功！" : resp.message;
                if(resp.status == 200) {
                    layer.open({
                        title: '温馨提示'
                        ,content: message
                    });
                }
            },
            error:function(XMLHttpRequest, status, errorThrown){}
        });
        return false;
    });


    form.on('checkbox(checkAll)', function(data){
        var _class = data.value;
        if (data.elem.checked) {
            $("." + _class).each(function(){
                $(this).prop("checked",true);
            });
            form.render('checkbox');
        } else {
            $("." + _class).each(function(){
                $(this).prop("checked",false);
            });
            form.render('checkbox');
        }
        form.render('checkbox');
    });

    var checkAll = function (data) {
        var _class = data.value.substr(0, data.value.lastIndexOf("-"));
        var item = $("." + _class);
        for (var i = 0; i < item.length; i++) {
            if (item[i].checked == false) {
                $("input[value='"+_class+"'").prop("checked", false);
                form.render('checkbox');
                return;
            }
        }
        //如果都勾选了  勾上全选
        var all = item.length;
        for (var i = 0; i < item.length; i++) {
            if (item[i].checked == true) {
                all--;
            }
        }
        if(all == 0){
            $("input[value='"+_class+"'").prop("checked", true);
            form.render('checkbox');
        }
    };

    //有一个未选中全选取消选中
    form.on('checkbox(checkOne)', checkAll);

    $(".checkAll").each(function(){
        checkAll({value : this.value + "-0"});
    })
    
    //对外暴露的接口
    exports('channel_servicearea_category', {});
});